**********************************************************************************************************************
*                        PLOT DECILE COEFFICIENTS FROM REGRESSIONS WITH HETEROGENEITY BY PREDICTED RISK              *
**********************************************************************************************************************


log using "${SIDCodePath}/sidsedd_regressions_predrisk_plotcoeffs.log", replace

local folders "reg_trad_predictedrisk_patientFEs"

foreach folder in `folders'{
	local files : dir "${SIDOutputPath}/`folder'/Coefficients//" files "*.dta"
	local path "${SIDOutputPath}/`folder'/Coefficients/"
	foreach file in `files' {
		use "`path'`file'", clear
		sum estimate if parm == "1.post_qtr_after_MN"
		local baseline_coeff = r(mean)
		sum min95 if parm  == "1.post_qtr_after_MN"
		local baseline_min95 = r(mean)

		sum max95 if parm  == "1.post_qtr_after_MN"
		local baseline_max95 = r(mean)

		keep if strpos(parm, "1.post_qtr") > 0 & strpos(parm, "#") > 0
		set obs 10
		
		gen decile = _n + 1
		replace estimate = `baseline_coeff' if decile == 11
		replace min95 = `baseline_min95' if decile == 11
		replace max95 = `baseline_max95' if decile == 11
		replace decile = 1 if decile == 11
		twoway (rcap min95 max95 decile) (scatter estimate decile), ///
				legend(off) xtitle("Predicted Risk Decile", size(large)) ytitle("") ///
				yline(0, lcolor(black) lpattern(dash)) graphregion(color(white)) ///
				xlabel(1(1)10, labsize(large)) ylabel(,labsize(large))

		local filename = substr("`file'", 1, length("`file'")-4)
		graph export "${SIDOutputPath}/`folder'/`filename'.eps", replace  
	}
}

log close